package com.begye.smartloan.apptest.renzheng.views

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.FillItemStatusBinding
import com.begye.smartloan.apptest.ext.addColor
import org.json.JSONObject

class FillItemStatus : RelativeLayout {

    private var _binding: FillItemStatusBinding? = null
    private val binding get() = _binding!!

    private var _clickListener: (String, String) -> Unit = { _, _ -> }

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        context?.let {
            val layout = View.inflate(it, R.layout.fill_item_status, this)
            _binding = FillItemStatusBinding.bind(layout)
        }
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
//        _binding =  FillItemStatusBinding.bind(this)

        // Ensure binding is not null after inflation
        if (_binding == null) {
            throw IllegalStateException("ViewBinding not initialized for ${javaClass.simpleName}")
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        _binding = null  // Clear the binding reference
    }

    fun initDataSource(value: JSONObject) {
        val title = value.optString("title", "")
        val tag = value.optString("tag", "")
        addTag(tag)
        addTitle(title)
    }

    fun addTag(value: String): FillItemStatus {
        this.tag = value
        return this
    }

    fun addCompleted(value: Boolean): FillItemStatus {
        if (value) {
//            binding.fillItemStatusIcon.visibility = View.VISIBLE
//            binding.fillItemStatusExt.visibility = View.GONE
            binding.fillItemStatusIcon.setImageResource(R.drawable.dres_51)
        } else {
//            binding.fillItemStatusIcon.visibility = View.GONE
//            binding.fillItemStatusExt.visibility = View.VISIBLE
//            binding.fillItemStatusExt.addUnderLine()
            binding.fillItemStatusIcon.setImageResource(R.drawable.dres_52)
        }
        return this
    }

    fun addTitle(value: String): FillItemStatus {
        binding.fillItemStatusTitle.text = value
        binding.fillItemStatusTitle.addColor("*", "#F55555")
        return this
    }

    fun addClickListener(listener: (String, String) -> Unit): FillItemStatus {
        _clickListener = listener
        binding.fillItemStatusExt.setOnClickListener {
            listener.invoke(
                this.tag.toString().trim(),
                binding.fillItemStatusTitle.text.toString().trim()
            )
        }
        return this
    }
}